package com.hamster.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hamster.crm.mapper.CourseTimeMapper;
import com.hamster.crm.mapper.ProjectCourseMapper;
import com.hamster.crm.pojo.Project;
import com.hamster.crm.pojo.ProjectCourse;
import com.hamster.crm.pojo.dto.CourseTime;
import com.hamster.crm.service.ProjectCourseService;
import com.hamster.crm.util.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

/**
 * @author Tarry
 * @create 2019/12/24 13:57
 */
@Service
public class ProjectCourseServiceImpl implements ProjectCourseService {
    @Autowired
    private ProjectCourseMapper projectCourseMapper;
    @Autowired
    private CourseTimeMapper courseTimeMapper;
    /**
     * 新增项目课程
     * @param projectCourse
     */
    @Transactional
    @Override
    public void insert(ProjectCourse projectCourse) {
        projectCourse.setId(null);//id置空null
        projectCourse.setDel("0");
        projectCourse.setInputUser("功能的待完善");
        projectCourse.setInputTime(new Date());
        projectCourseMapper.insert(projectCourse);//插入课程表
        //获取课程时间,批量插入课程时间表
        List<CourseTime> courseTime = projectCourse.getCourseTime();
        for (CourseTime time : courseTime) {
            time.setCourseId(projectCourse.getId());
            courseTimeMapper.insert(time);
        }
    }

    /**
     * 删除项目课程
     * @param ids
     */
    @Transactional
    @Override
    public void delete(List<String> ids) {
        for (String id : ids) {
            projectCourseMapper.deleteTag(id);
        }
    }

    /**
     * 获取项目课程详情
     * @param id
     * @return
     */
    @Override
    public ProjectCourse get(String id) {
        return projectCourseMapper.getCourseDetail(id);
    }

    /**
     * 修改项目课程
     * @param projectCourse
     */
    @Transactional
    @Override
    public void update(ProjectCourse projectCourse) {
        projectCourse.setInputTime(new Date());
        projectCourse.setInputUser("功能待完善");
        projectCourseMapper.updateById(projectCourse);
        //修改课程对应的上课时间
        //根据id先删除旧记录，再插入新纪录
        courseTimeMapper.deleteCourseTimeByCourseId(projectCourse.getId());
        //批量插入课程时间表
        List<CourseTime> courseTime = projectCourse.getCourseTime();
        for (CourseTime time : courseTime) {
            time.setCourseId(projectCourse.getId());
            courseTimeMapper.insert(time);
        }
    }

    /**
     * 分页条件查询项目课程
     * @param pageSize
     * @param currentPage
     * @param title
     * @return
     */
    @Override
    public PageResult<ProjectCourse> listProjectCourses( Integer pageSize, Integer currentPage, String title) {

        QueryWrapper<ProjectCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del","0").orderByDesc("id");
        if (StringUtils.isNotBlank(title)){
            queryWrapper.like("title","%"+title+"%");
        }
        IPage pageList = projectCourseMapper.selectPage(new Page<ProjectCourse>(currentPage,pageSize), queryWrapper);
        List<ProjectCourse> currentList = pageList.getRecords();
        Long pages = pageList.getPages();
        return new PageResult<ProjectCourse>(pageList.getTotal(),pages.intValue(),currentList);
    }

    /**
     * 下拉框获取项目课程列表
     * @return
     */
    @Override
    public List<ProjectCourse> listOptions() {
        QueryWrapper<ProjectCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del","0");
        return projectCourseMapper.selectList(queryWrapper);

    }

    /**
     * 根据课程id集合，查询课程【列表】
     * @param courseList
     * @return
     */
    @Override
    public List<ProjectCourse> queryCourse(List<String> courseList){
        return projectCourseMapper.selectBatchIds(courseList);
    }



}
